﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DealerJudge.Entity;
using DealerJudge.Web.Models;

namespace DealerJudge.Web.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            HomeModel homeModel = new HomeModel();

            //category
            List<Category> categoryList = homeModel.GetDealerCategory();
            ViewBag.categoryList = categoryList;

            //All Cities
            List<City> cityList = homeModel.GetAllCities();
            ViewData["cityList"] = new SelectList(cityList, "Id", "Name");
            ViewBag.ListCity = cityList;

            //City By Category
            List<City> cityListByCategory = homeModel.GetCitiesByCategory(1);
            ViewData["cityListByCategory"] = new SelectList(cityListByCategory, "Id", "Name");

            //Manucafturer By City and Category
            List<Manufacturer> ManufacturerListByCategoryAndCity = homeModel.GetManufacturerByCityAndCategory(1, 1);
            ViewData["ManufacturerListByCategoryAndCity"] = new SelectList(ManufacturerListByCategoryAndCity, "Id", "Name");
            
            //test
            List<Category> catList = homeModel.GetServiceCenterCategory();
            ViewData["catList"] = catList;
            return View();
        }

        [HttpPost]
        public ActionResult GetManufacturerForCityAndCategory(int city_id = 1, int cat_id = 0)
        {
            try
            {
                //in real application made a better load / retrieving
                HomeModel homeModel = new HomeModel();

                List<Manufacturer> ManufacturerListByCategoryAndCity = homeModel.GetManufacturerByCityAndCategory(city_id, cat_id);
                //ViewData["ManufacturerListByCategoryAndCity"] = new SelectList(ManufacturerListByCategoryAndCity, "Id", "Name");

                return Json(new { ok = true, data = ManufacturerListByCategoryAndCity, message = "ok" });
            }
            catch (Exception ex)
            {
                return Json(new { ok = false, message = ex.Message });
            }
        }

        
        public ActionResult About()
        {
            return View();
        }
    }
}
